﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Globalization;

public partial class WebUserControl_CTrChotdoigiaban : System.Web.UI.UserControl
{
    BCDataCenter.DataAccessLayer.DAL dt;
    private static string trntyp = "";
    private static string sitecode = "";
    private static string location = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            sitecode = Session["Sitecode"].ToString();
            string error = "";
            DataSet Ds = new DataSet();
            dt = new BCDataCenter.DataAccessLayer.DAL();
            Ds = dt.MyExecuteReader(ref error, "select location from INLOCD where sitecode ='" + sitecode + "'", CommandType.Text);
            location = Ds.Tables[0].Rows[0][0].ToString();
            getIntxtype();            
            RefreshList_Intx();
            trntyp = Ddl_loainghiepvu.SelectedValue.ToString();
            //-------------------            
            Ds = dt.MyExecuteReader(ref error, "SELECT     [Closed Date] FROM mis where sitecode ='" + sitecode + "'", CommandType.Text);
            string tmp = Ds.Tables[0].Rows[0][0].ToString();
            DateTime dateTest = DateTime.Parse(tmp, new CultureInfo("fr-FR", false));
            dateTest = dateTest.AddDays(double.Parse("1"));
            txt_ngay.Text = dateTest.ToString("dd/MM/yyyy");
        }
    }
    private void RefreshList_Intx()
    {
        string error = "";
        dt = new BCDataCenter.DataAccessLayer.DAL();
        DataSet ds = new DataSet();
        string date = txt_ngay.Text.ToString();
        if (trntyp != "")
        {
            ds = dt.MyExecuteQuery(ref error, "get_intxtd", CommandType.StoredProcedure, new SqlParameter("@trntyp", trntyp), new SqlParameter("@docdate", date),new SqlParameter("@sitecode",sitecode));
            Gv_Nghiepvu.DataSource = ds.Tables[0];
            Gv_Nghiepvu.DataBind();
        }
        Gv_Nghiepvu.Columns[2].ItemStyle.Width = 180;
    }
    private void getIntxtype()
    {
        string error = "";
        dt = new BCDataCenter.DataAccessLayer.DAL();
        DataSet DS;
        DS = dt.MyExecuteReader(ref error, "get_Intxtyp", CommandType.StoredProcedure);
        //DS = dt.MyExecuteReader(ref error, "get_inlocd", CommandType.StoredProcedure);
        Ddl_loainghiepvu.DataSource = DS.Tables[0];
        Ddl_loainghiepvu.DataTextField = "description";
        Ddl_loainghiepvu.DataValueField = "trntyp";
        Ddl_loainghiepvu.DataBind();
    }
    protected void bt_chot_Click(object sender, EventArgs e)
    {
        dt = new BCDataCenter.DataAccessLayer.DAL();
        bool kq = false;
        string ketqua0 = "";
        string ketqua = "";
        string error = "";
        //lb_ketqua.Text = "";
        //lb_ketqua0.Text="";
        bool flag = false;
        for (int i = 0; i < Gv_Nghiepvu.Rows.Count; i++)
        {                                
            GridViewRow row = Gv_Nghiepvu.Rows[i];
            bool ischecked = ((CheckBox)row.FindControl("checkbox1")).Checked;
            if (ischecked)
            {
                string tem = "";
                if (Gv_Nghiepvu.Rows[i].Cells[3].Text.ToString() == "0" || Gv_Nghiepvu.Rows[i].Cells[3].Text.ToString() == "false")
                {
                    flag = true;
                    tem = Gv_Nghiepvu.Rows[i].Cells[1].Text.ToString();
                    ketqua0 = ketqua0 + "," + tem.Substring(tem.Length - 4, 4);
                }
                else 
                {
                    string docno = Gv_Nghiepvu.Rows[i].Cells[1].Text.ToString();
                    kq = dt.MyExecuteNonQuery("post_Intxd", CommandType.StoredProcedure, ref error, new SqlParameter("@trntyp", trntyp), new SqlParameter("@location", location), new SqlParameter("@docno", docno));
                    if (kq)
                    {
                        ketqua = ketqua + "," + docno.Substring(docno.Length - 4, 4);
                    }
                            
                }
            }
        }
        if (ketqua == "" && ketqua0 == "")
            uscMsgBox1.AddMessage("Bạn không chọn CT nào để chốt", uscMsgBox.enmMessageType.Error);
        else
        {
            if (flag)
            {
                uscMsgBox1.AddMessage("Số CT " + ketqua0 + " không thể chốt nghiệp vụ vì chưa in", uscMsgBox.enmMessageType.Error);
            }
            if (ketqua != "")
            {
                RefreshList_Intx();
                uscMsgBox1.AddMessage("Số CT " + ketqua + "da chot", uscMsgBox.enmMessageType.Success);
            }
        }
    }
    protected void Ddl_loainghiepvu_SelectedIndexChanged(object sender, EventArgs e)
    {       
        trntyp = Ddl_loainghiepvu.SelectedValue.ToString();
        RefreshList_Intx();
    }

    
}
